package exp5.Part4;


import week12.Sorting;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * Created by besti on 2018/6/9.
 */
public class SocketServer {
    public static void main(String[] args) throws IOException {
        //1.建立一个服务器Socket(ServerSocket)绑定指定端口
        ServerSocket serverSocket=new ServerSocket(8800);
        //2.使用accept()方法阻止等待监听，获得新连接
        Socket socket=serverSocket.accept();
        //3.获得输入流
        InputStream inputStream=socket.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
        //获得输出流
        OutputStream outputStream=socket.getOutputStream();
        PrintWriter printWriter=new PrintWriter(outputStream);
        //4.读取用户输入信息
        String info=null;
        Sorting sorting = new Sorting();
        String temp = "";
        MyDC dc = new MyDC();
        while(!((info = bufferedReader.readLine()) ==null)){
            System.out.println("我是服务器，接收到的是：" + info);
            temp = info;
        }
        String p = "";

        try {

            // 读取对方的DH公钥
            FileInputStream f1=new FileInputStream("CPub.dat");
            ObjectInputStream b1=new ObjectInputStream(f1);
            PublicKey pbk=(PublicKey)b1.readObject( );
            //读取自己的DH私钥
            FileInputStream f2=new FileInputStream("SPri.dat");
            ObjectInputStream b2=new ObjectInputStream(f2);
            PrivateKey prk=(PrivateKey)b2.readObject( );
            // 执行密钥协定
            KeyAgreement ka=KeyAgreement.getInstance("DH");
            ka.init(prk);
            ka.doPhase(pbk,true);
            //生成共享信息
            byte[ ] sb=ka.generateSecret();
            System.out.print("共享信息：");
            for(int i=0;i<sb.length;i++){
                System.out.print(sb[i]+",");
            }
            SecretKeySpec k1 = new  SecretKeySpec(sb,"DESede");
            System.out.println();


            String[] a = temp.split(",");
            byte[] ab = new byte[a.length];
            for (int i = 0; i < a.length; i++) {
                ab[i] = Byte.parseByte(a[i]);
            }
            FileInputStream f3 = new FileInputStream("keykb1.dat");
            int num2 = f3.available();
            byte[] keykb = new byte[num2];
            f3.read(keykb);
            SecretKeySpec k = new SecretKeySpec(keykb, "DESede");
            Cipher cp = Cipher.getInstance("DESede");
            cp.init(Cipher.DECRYPT_MODE, k);
            byte[] ptext = cp.doFinal(ab);
            p = new String(ptext, "UTF8");
            System.out.println(p);
            int result = dc.evaluate(p);
            System.out.println("结果是：" + result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //给客户一个响应
        String reply = String.valueOf(dc.evaluate(p));
        printWriter.write(reply);
        printWriter.flush();
        //5.关闭资源
        printWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}
